package leetcode.秋招.华为OD;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/9/19 23:00
 * @description：
 */
public class Main3 {
    static List<Integer> list = new LinkedList<>();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < N; i++) {
            int key = in.nextInt();
            int val = in.nextInt();
            map.put(key, val);
        }
        int delID = in.nextInt();
        list.add(delID);

        int searchI = 0;
        Set<Integer> set = map.keySet();
        while (searchI < list.size()) {
            for (Integer key : set) {
                if (list.get(searchI) - map.get(key) == 0) {
                    list.add(key);
                }
            }
            searchI++;
        }

        List<Integer> finalList = new ArrayList<>();
        for (Integer key : set) {
            if (!list.contains(key)) {
                finalList.add(key);
            }
        }
        finalList.sort(Integer::compareTo);
        for (int i = 0; i < finalList.size(); i++) {
            if (i != finalList.size() - 1) {
                System.out.print(finalList.get(i) + " ");
            } else {
                System.out.print(finalList.get(i));
            }
        }
    }
}
